package com.tan.lbs.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.tan.lbs.dao.MediaDAO;
import com.tan.lbs.entity.Item;
import com.tan.lbs.entity.Location;
import com.tan.lbs.entity.Media;
import com.tan.lbs.util.HibernateUtil;

public class MediaDAOImpl implements MediaDAO{

	@Override
	public String create(Media media) {
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Session s = sf.getCurrentSession();
		s.beginTransaction();
		s.save(media);
		s.getTransaction().commit();
		return media.getMediaID();
	}


		@Override
		public Media readByID(String mediaID) {
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session s = sf.getCurrentSession();
			s.beginTransaction();
			Media media = (Media)s.get(Media.class, mediaID);
			s.getTransaction().commit();
			return media;			
		}
		
		@Override
		public void delete(String mediaID) {
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session s = sf.getCurrentSession();
			s.beginTransaction();
			Media media = new Media();
			media.setMediaID(mediaID);
			s.delete(media);
			s.getTransaction().commit();		
		}


		@Override
		public ArrayList<Media> searchByGame(String gameID) {
			SessionFactory sf = HibernateUtil.getSessionFactory();
			Session s = sf.getCurrentSession();
			s.beginTransaction();
			List<Media> gs = s.createCriteria(Media.class).add(Restrictions.
					eq("gameID", gameID)).list();
			s.getTransaction().commit();
			return new ArrayList<Media>(gs);
		}
		

}
